import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';
import 'dart:async';


class RxDartDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title:Text('RxDartDemo'),
          elevation: 0.0,
        ),
        body:RxDartHomeDemo(),

        );
  }
}
class RxDartHomeDemo extends StatefulWidget {
  @override
  _RxDartHomeDemoState createState() => _RxDartHomeDemoState();
}

class _RxDartHomeDemoState extends State<RxDartHomeDemo> {

  PublishSubject <String> _textFieldSubject;

   @override
  void initState() {
    // TODO: implement initState
    super.initState();
 
   // Observable<String>_observable = Observable.periodic(Duration(seconds:3),(x) => x.toString());
    //Observable.just('hello~');
    //Observable(Stream.fromIterable(['Hello','你好']));
    //_observable.listen(print);

    // PublishSubject<String> _subject = PublishSubject<String>();
    // _subject.listen((data) => print('listen1: $data'));
    // _subject.add('hello');
    // _subject.listen((data) => print('listen2: ${data.toUpperCase()}'));
    // _subject.add('hellotest');
    // _subject.close();

       _textFieldSubject = PublishSubject <String>();
       //_textFieldSubject.listen((data) => print('listen1: $data'));

       //_textFieldSubject.map((item) => 'item: $item').listen((data) => print('listen1: $data'));
       //_textFieldSubject.where((item) => item.length > 9).listen((data) => print('listen1: $data'));
       _textFieldSubject.debounce((Duration(milliseconds: 500))).listen((data) => print('listen1: $data'));

  }
@override
  void dispose() {
    // TODO: implement dispose
    _textFieldSubject.close();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Theme(
      data: Theme.of(context).copyWith(
        primaryColor: Colors.black
      ),
      child:Center(
        child:TextField(
          onChanged:(value){
            _textFieldSubject.add('Input: $value');
          },
          onSubmitted:(value){
             _textFieldSubject.add('Submitted: $value');
          },
          decoration: InputDecoration(
           labelText:'title',
           filled: true
          ),
        )
      ),
      );
  }
}